//
//  WPVisitorView.swift
//  WB
//
//  Created by 汪鹏 on 2016/12/21.
//  Copyright © 2016年 汪鹏. All rights reserved.
//

import UIKit

class WPVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// MARK:--配置界面的数据,给控件设置数据
    /// 该字典中包含两个字段：imageName,message
    var visitorInforData:[String:String]?{
        didSet{
          
            guard let imageName = visitorInforData?["imageName"],
                let message = visitorInforData?["message"]else{
                  
                 return
            }
            
            
            tipTitle_View.text = message
            
            if imageName == "" {
                
                startAnimating()
                
                return
            }
            
            icon_View.image = UIImage(named: imageName)
            
            maskIcon_View.isHidden = true
            
            houseicon_View.isHidden = true
            
        }
    }
    
    /// MARK:--懒加载私有的控件（懒加载的是UIKit的控件的构造函数，不需要给变量填加类型，其他情况要加上）
    lazy var icon_View:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var maskIcon_View:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var houseicon_View:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var tipTitle_View:UILabel = UILabel.cz_label(withText: "庙小妖风重，池浅王八多,庙小妖风重，池浅王八多",
                                                              fontSize: 16,
                                                              color: UIColor.darkGray)
    
    public var regist_Button:UIButton = UIButton.cz_textButton("注册",
                                                                     fontSize: 16,
                                                                     normalColor: UIColor.orange,
                                                                     highlightedColor: UIColor.black,
                                                                     backgroundImageName: "common_button_white_disable")
    
    public var login_Button:UIButton = UIButton.cz_textButton("登陆",
                                                                     fontSize: 16,
                                                                     normalColor: UIColor.darkGray,
                                                                     highlightedColor: UIColor.black,
                                                                     backgroundImageName: "common_button_white_disable")
    
    
 
}

// MARK:--设置首页动画
extension WPVisitorView {
    
    func startAnimating() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.duration = 15
        
        anim.repeatCount = MAXFLOAT
        
        // 动画完成不删除，当icon_View == nil时，动画也被移除
        anim.isRemovedOnCompletion = false
        
        icon_View.layer.add(anim, forKey: nil)
        
    }
}

// MARK:--设置界面
extension WPVisitorView {
    
    func setupUI() {
      
        backgroundColor = UIColor.cz_color(withRed: 232, green: 233, blue: 232)
        
        // 添加组件
        addSubview(icon_View)
        
        addSubview(maskIcon_View)
        
        addSubview(houseicon_View)
        
        addSubview(tipTitle_View)
        
        addSubview(regist_Button)
        
        addSubview(login_Button)
        
        // 设置tipTitle_View的文字居中
        tipTitle_View.textAlignment = .center
        
        // 取消所有组件的Autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 添加约束，自动布局
        
        let margin:CGFloat = 20.0
        
        addConstraint(NSLayoutConstraint(item: icon_View, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)) // icon_View的centerX
        
        addConstraint(NSLayoutConstraint(item: icon_View, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)) // icon_View的centerY
        
        addConstraint(NSLayoutConstraint(item: houseicon_View, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)) // houseicon_View的centerX
        
        addConstraint(NSLayoutConstraint(item: houseicon_View, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)) // houseicon_View的centerX
        
        addConstraint(NSLayoutConstraint(item: tipTitle_View, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)) // tipTitle_View的centerX
        
        addConstraint(NSLayoutConstraint(item: tipTitle_View, attribute: .top, relatedBy: .equal, toItem: icon_View, attribute: .bottom, multiplier: 1, constant: margin)) // tipTitle_View的y
        
        addConstraint(NSLayoutConstraint(item: tipTitle_View, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 236)) // tipTitle_View的width
        
        addConstraint(NSLayoutConstraint(item: regist_Button, attribute: .left, relatedBy: .equal, toItem: tipTitle_View, attribute: .left, multiplier: 1, constant: 0)) // regist_Button的X
        
        addConstraint(NSLayoutConstraint(item: regist_Button, attribute: .top, relatedBy: .equal, toItem: tipTitle_View, attribute: .bottom, multiplier: 1, constant: margin)) // regist_Button的y
        
        addConstraint(NSLayoutConstraint(item: regist_Button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)) // regist_Button的width
        
        addConstraint(NSLayoutConstraint(item: login_Button, attribute: .right, relatedBy: .equal, toItem: tipTitle_View, attribute: .right, multiplier: 1, constant: 0)) // login_Button的X
        
        addConstraint(NSLayoutConstraint(item: login_Button, attribute: .top, relatedBy: .equal, toItem: tipTitle_View, attribute: .bottom, multiplier: 1, constant: margin)) // login_Button的y
        
        addConstraint(NSLayoutConstraint(item: login_Button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)) // login_Button的width

        // 用VFL布局maskIcon_View
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIcon_View]-0-|", options: [], metrics: nil, views: ["maskIcon_View":maskIcon_View])) // maskIcon_View的X
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(-300)-[maskIcon_View]-0-|", options: [], metrics: nil, views: ["maskIcon_View":maskIcon_View])) // maskIcon_View的Y
        
    }
    
}
